package com.admin.service.impl;

import cn.hutool.core.util.RandomUtil;
import com.admin.entity.StationKeyRecharge;
import com.admin.entity.TerminalKey;
import com.admin.excelList.TerminalListkey;
import com.admin.mapper.TerminalKeyMapper;
import com.admin.service.ITerminalKeyService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author marisheng
 * @since 2019-11-21
 */
@Service
public class TerminalKeyServiceImpl extends ServiceImpl<TerminalKeyMapper, TerminalKey> implements ITerminalKeyService {

    @Autowired
    @SuppressWarnings("all")
    private TerminalKeyMapper terminalKeyMapper;

    @Autowired
    private StringRedisTemplate  redisTemplate;

    /**
     * 查看该终端下已过期和使用中的密钥信息  和   根据开始时间和结束时间进行条件模糊查询详情
     * @param page
     * @param limit
     * @param map 终端编号  开始时间  结束时间
     * @return
     */
    @Override
    public IPage findByTimeORFindAll(int page, int limit, Map map) {
        Page p = new Page(page, limit);
        IPage list = terminalKeyMapper.findByTimeORFindAll(p,map);
        return list;
    }

    /**
     * 过站点编号 查询站点下面所有未使用的密钥
     * @param station_id 站点编号
     * @return
     */
    @Override
    public List<Map> selectTerminalPasswordById(Integer station_id) {
        List<Map> list = terminalKeyMapper.selectTerminalPasswordById(station_id);
        return list;
    }

    /**
     * 查找密钥  没有密钥直接return 有则进入密钥列表
     * @param terminal_id
     * @return
     */
    @Override
    public String checkTerminalPassword(Integer terminal_id) {
        String miYao = terminalKeyMapper.checkTerminalPassword(terminal_id);
        return miYao;
    }
    /**
     * 根据站点编号来查询站点下的所有密钥  并做分页根据搜索条件
     * @param map   查询条件
     * @return
     */
    @Override
    public IPage findByStationIdQueryPassword(int page, int limit, Map map){
        Page page1= new Page(page,limit);
        IPage byStationIdQueryPassword = terminalKeyMapper.findByStationIdQueryPassword(page1, map);
        return byStationIdQueryPassword;
    }

    @Override
    public IPage findByStationIdQueryPasswordExcel(int page, int limit, Map map) {
        Page page1= new Page(page,limit);
        IPage byStationIdQueryPassword = terminalKeyMapper.findByStationIdQueryPasswordExcel(page1, map);
        return byStationIdQueryPassword;
    }

    /**
     * 根据站点id 来查询终端密钥为空的
     * @param page
     * @param map
     * @return
     */
    @Override
    public  IPage findByStationIdQueryTerminal(int page, int limit, Map map){
        Page page1= new Page(page,limit);
        IPage byStationIdQueryTerminal = terminalKeyMapper.findByStationIdQueryTerminal(page1, map);
        return byStationIdQueryTerminal;
    }

    /**
     * 根据前台传过来的数量在密钥表新增数据
     */
    @Override
    public int insertTermianlKeySumAll(StationKeyRecharge skr){
        TerminalKey terminalKey = new TerminalKey();
        terminalKey.setKeySite(skr.getStationKeyRechargeStation());
        terminalKey.setTerminalKeyStatus("未使用");
        int a=0;
        for(int i=0;i<skr.getStationKeyRechargeSum();i++){
            //产生6位数的随机密钥
            String code = RandomUtil.randomNumbers(6);
            terminalKey.setTerminalKeyValue(code);
            a= terminalKeyMapper.insert(terminalKey);
        }
        return a;
    }

    /**
     * 修改上线时间和下线时间 还有修改状态 还需算出时间差 然后累加入总时长
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @param terminal_key 终端密钥外键（终端主键）
     * @param id 终端密钥id
     * @return
     */
    @Override
    public int updateKeyTimeAndStatus(String startTime,String endTime,Integer terminal_key,Integer id){
        //把datetime的数据转化成时间戳 算出时间差
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date start = null;
        Date end=null;
        try {
            start = sdf.parse(startTime);
            end=sdf.parse(endTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        long newStart = start.getTime();
        long newEnd=end.getTime();

        //时间差的时间戳转化成分钟
        int cha= (int) (newEnd-newStart)/60/1000;
        updateTotalTime(cha, terminal_key);

        //******都成功后 存入redis****
        redisTemplate.boundValueOps("miyaoKey_"+id).set("0",1,TimeUnit.MINUTES);

        //修改时间和状态
        int n = terminalKeyMapper.updateKeyTimeAndStatus(startTime,endTime,terminal_key,id);
        return n;
    }

    @Override
    public int updatePassword(String terminal_key_value, Integer terminal_id,Integer terminal_key_id) {
        int n = terminalKeyMapper.updatePassword(terminal_key_value, terminal_key_id,terminal_id);
        return n;
    }

    @Override
    public int updatePasswordRoom( Integer terminal_key_id,Integer terminal_key_room,String terminal_key_room_type) {

        int n = terminalKeyMapper.updatePasswordRoom(terminal_key_id,terminal_key_room,terminal_key_room_type);
        return n;
    }

    /**
     * 终端授权  调用了上面的两个方法
     * @param
     */
    @Override
    @Transactional
    public int terminalShouQuan(Integer terminal_key_id,Integer terminal_id,String terminal_key_value,String terminal_key_begin_time,String terminal_key_end_time,String terminal_room){
        //这个是修改密钥表的
        int i1 = updateKeyTimeAndStatus(terminal_key_begin_time, terminal_key_end_time,terminal_id, terminal_key_id);
        int i = updatePassword(terminal_key_value, terminal_id,terminal_key_id);
        int i2 = updateKeyTimeAndStatus2(terminal_key_id,terminal_room);
        if(i>0 & i1>0 & i2>0){
            return 1;
        }else {
            return 0;
        }
    }

    @Override
    @Transactional
    public int terminalShouQuanRoom(Integer terminal_key_id,Integer terminal_id,String terminal_key_value,String terminal_key_begin_time,String terminal_key_end_time,Integer terminal_key_room,String terminal_key_room_type){

        int i1 = updateKeyTimeAndStatus(terminal_key_begin_time, terminal_key_end_time,terminal_id, terminal_key_id);
        int i = updatePasswordRoom(terminal_key_id,terminal_key_room,terminal_key_room_type);
        int i2 = updatePassword(terminal_key_value, terminal_id,terminal_key_id);

        if(i>0 & i1>0 & i2>0){
            return 1;
        }else {
            return 0;
        }
    }
    /**
     * 查询所有密钥分页显示
     * @param page
     * @param limit
     * @param map
     * @return
     */
    @Override
    public IPage selectTerminalKeys(int page, int limit, Map map) {
        Page page1 = new Page(page, limit);
        return terminalKeyMapper.selectTerminalKeys(page1, map);
    }

    @Override
    public int updateTotalTime(int cha, int id){
        return terminalKeyMapper.updateTotalTime(cha,id);
    }

    @Override
    public int updateKeyTimeAndStatus2(Integer terminal_key_id, String terminal_room) {
        int i = terminalKeyMapper.updateKeyTimeAndStatus2(terminal_room, terminal_key_id);
        return i;
    }

    @Override
    public List<TerminalListkey> selectTerminalKeysExcel() {


        return terminalKeyMapper.selectTerminalKeysExcel();
    }
}
